package com.example.tbtx.tbtx.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerAdapterWrapper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.tbtx.tbtx.HomeActivity;
import com.example.tbtx.tbtx.MainActivity;
import com.example.tbtx.tbtx.R;
import com.example.tbtx.tbtx.module.AddCollectResponse;
import com.example.tbtx.tbtx.module.MainListResponse;
import com.example.tbtx.tbtx.retrofit.NetCallBack;
import com.example.tbtx.tbtx.retrofit.RetrofitHelper;
import com.example.tbtx.tbtx.ui.CompanyDetailActivity;
import com.example.tbtx.tbtx.utils.MaskUtil;
import com.example.tbtx.tbtx.utils.system.SystemUtil;
import com.example.tbtx.tbtx.view.CloudProgressView;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MainPagerAdapter extends PagerAdapter implements CloudProgressView.StartActivityCallBack {


    private final Context context;
    private final ArrayList<MainListResponse.DatasBean> list;
    private int position;

    public MainPagerAdapter(ArrayList<MainListResponse.DatasBean> list, Context context) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }


    @SuppressLint("CheckResult")
    @NonNull
    @Override
    public Object instantiateItem(@NonNull final ViewGroup container, final int position) {
        final View view = LayoutInflater.from(context).inflate(R.layout.view_empty, container, false);
        final CheckBox iv_like = (CheckBox) view.findViewById(R.id.iv_like);
        final MainListResponse.DatasBean bean = list.get(position);
        String[] split = bean.getEd_image().split(",");
        String ed_image = split[0];
        Glide.with(context).load(bean.getEd_logo()).into((ImageView) view.findViewById(R.id.iv_bg));
        iv_like.setChecked(bean.isCollectValue());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Build.VERSION.SDK_INT < 23 || Settings.canDrawOverlays(context)) {
                    new MaskUtil(context, MainPagerAdapter.this);
                    MainPagerAdapter.this.position = position;
                } else {
//                    ((MainActivity) context).getPermission();
                    Toast.makeText(context, "请开启悬浮窗权限", Toast.LENGTH_SHORT).show();
                }
            }
        });
        iv_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iv_like.isChecked()) {
                    String deviceID = SystemUtil.getDeviceID(context);
                    RetrofitHelper.getApi().addCollection(((MainActivity) context).getID(), bean.getEd_id() + "", deviceID)
                            .enqueue(new NetCallBack<AddCollectResponse>(context) {
                                @Override
                                public void onSuccess(Call<AddCollectResponse> call, Response<AddCollectResponse> response) {
                                    if ("success".equals(response.body().getMsgType())) {
                                        iv_like.setChecked(true);
                                        Toast.makeText(context, "收藏成功了", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                } else {

                    String deviceID = SystemUtil.getDeviceID(context);

                    RetrofitHelper.getApi().delCollecttion(((MainActivity) context).getID(), bean.getEd_id() + "", deviceID)
                            .enqueue(new NetCallBack<AddCollectResponse>(context) {

                                @Override
                                public void onSuccess(Call<AddCollectResponse> call, Response<AddCollectResponse> response) {
                                    if ("success".equals(response.body().getMsgType())) {
                                        iv_like.setChecked(false);
                                        Toast.makeText(context, "您已取消收藏", Toast.LENGTH_SHORT).show();

                                    }
                                }
                            });
                }

            }
        });
//        ((TextView) view.findViewById(R.id.text)).setText(list.get(position).getEd_name());
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public void startActivity() {
        Intent intent = new Intent(context, CompanyDetailActivity.class);
        intent.putExtra("detail", list.get(position));
        context.startActivity(intent);
    }
}
